package com.mmy.service.group.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mmy.controller.UtilConstants;
import com.mmy.dao.group.TGroupEateryRepository;
import com.mmy.dao.group.TGroupHotelRepository;
import com.mmy.dao.group.TGroupLocalRepository;
import com.mmy.dao.group.TGroupProjectRepository;
import com.mmy.dao.group.TGroupSceneryRepository;
import com.mmy.domain.group.TGroupEatery;
import com.mmy.domain.group.TGroupHotel;
import com.mmy.domain.group.TGroupLocal;
import com.mmy.domain.group.TGroupProject;
import com.mmy.domain.group.TGroupScenery;
import com.mmy.mapper.GroupProjectDao;
import com.mmy.pojo.line.GroupForm;
import com.mmy.pojo.line.Hotels;
import com.mmy.pojo.line.Locals;
import com.mmy.pojo.line.Rests;
import com.mmy.pojo.line.Scenics;
import com.mmy.service.ServiceCommon;
import com.mmy.util.CodeUtil;

/**
 * 
 * @vesion 
 * @description 处理基本表单以及地接/餐馆/景点/酒店
 * @author liys
 * @email 21555@sohu.com
 * @date 2021-7-5
 */

@Service
public class GroupProjectServiceImpl {
	
	@Autowired
	ServiceCommon common;
	@Autowired
	TGroupProjectRepository projectDao;
	@Autowired
	GroupProjectDao projectMapper;
	@Autowired
	TGroupLocalRepository localDao;
	@Autowired
	TGroupHotelRepository hotelDao;
	@Autowired
	TGroupSceneryRepository sceneryDao;
	@Autowired
	TGroupEateryRepository eateryDao;
	
	/**
	  * @descr 获得最大编号 
	  * @param 
	  * @return 
	  * @throws 
	  * @author liys
	  * @date 2021-6-1
	 */
	public int getMaxID() {
		 Object objCode = projectDao.getMaxID();
		 return CodeUtil.MaxID(objCode);
	 }
	
	/**
	  * @descr 保存计划单
	  * @param 
	  * @return 
	  * @throws 
	  * @author liys
	  * @date 2021-6-1
	 */
	public int save(GroupForm groupForm,int iOrgID) {
		
		int flag = 1;
		
		TGroupProject pro = new TGroupProject();
		BeanUtils.copyProperties(groupForm,pro);
		//计划单编号
		int iPlanID = common.createBaseCode(UtilConstants.TableName.T_GROUP_PROJECT);
		String date = groupForm.getDate();
		String lineID = groupForm.getLineID();
		String dateNum = date.replaceAll("-","");
		String iTeamID = lineID + dateNum;
		//主键
		pro.setPlanID(iPlanID);
		//团队编号
		pro.setTeamID(iTeamID);
		//车辆信息
		pro.setCarID(groupForm.getCarID()[1]);
		//公司编号
		pro.setSpare1(iOrgID);
		projectDao.insert(iPlanID);
		projectDao.save(pro);
		
		Locals[] localArr = groupForm.getGrouds();
		Scenics[] scenicArr = groupForm.getScenics();
		Hotels[] hotelArr = groupForm.getHotels();
		Rests[] restArr = groupForm.getRests();
		
		//地接
		for (Locals localItem : localArr) {
			TGroupLocal tGroupLocal = new TGroupLocal();
			int id = common.createBaseCode(UtilConstants.TableName.T_GROUP_LOCAL);
			tGroupLocal.setGroudID(id);
			tGroupLocal.setKeyID(iPlanID);
			tGroupLocal.setGroundName(localItem.getGroudName());
		    tGroupLocal.setGroundTel(localItem.getGroudTel());
		    localDao.insert(id);
		    localDao.save(tGroupLocal);
		}
		
		//景点
		for (Scenics scenicItem : scenicArr) {
			TGroupScenery tGroupScenery = new TGroupScenery();
			tGroupScenery.setSceneryID(common.createBaseCode(UtilConstants.TableName.T_GROUP_SCENERY));
			tGroupScenery.setKeyID(iPlanID);
			tGroupScenery.setPlanID(scenicItem.getPlanID()[1]);
			tGroupScenery.setNum(scenicItem.getNum());
			tGroupScenery.setTotal(scenicItem.getTotal());
			sceneryDao.insert(tGroupScenery.getSceneryID());
			sceneryDao.save(tGroupScenery);
		}
		
		//酒店
		for (Hotels hotelItem : hotelArr) {
			TGroupHotel tGroupHotel = new TGroupHotel();
			tGroupHotel.setHotelID(common.createBaseCode(UtilConstants.TableName.T_GROUP_HOTEL));
			tGroupHotel.setKeyID(iPlanID);
			tGroupHotel.setPlanID(hotelItem.getPlanID()[1]);
			tGroupHotel.setNum(hotelItem.getNum());
			tGroupHotel.setTotal(hotelItem.getTotal());
			hotelDao.insert(tGroupHotel.getHotelID());
			hotelDao.save(tGroupHotel);
		}
		
		//餐馆
		for (Rests restItem : restArr) {
			TGroupEatery tGroupEatery = new TGroupEatery();
			tGroupEatery.setRestaurantID(common.createBaseCode(UtilConstants.TableName.T_GROUP_EATERY));
			tGroupEatery.setKeyID(iPlanID);
			tGroupEatery.setPlanID(restItem.getPlanID()[1]);
			tGroupEatery.setNum(restItem.getNum());
			tGroupEatery.setTotal(restItem.getTotal());
			eateryDao.insert(tGroupEatery.getRestaurantID());
			eateryDao.save(tGroupEatery);
		}
		
		return flag;
	}
	
	/**
	  * @descr 已出团列表数据
	  * @param 
	  * @return 
	  * @throws 
	  * @author liys
	  * @date 2021-7-5
	 */
	public List<Map<String,Object>> getGroups(int iOrgID,int count,int index){
		
		return projectMapper.getGroups(iOrgID, count, index);
	}
	
	public int getGroupCount(int iOrgID) {
		
		return projectMapper.getGroupCount(iOrgID);
	}
}
